package main

import (
	"babydoge/config"
	"babydoge/operators"
	"babydoge/processor"
	"babydoge/routers"
	"flag"
	"fmt"
	"github.com/labstack/echo/v4"
	"github.com/labstack/echo/v4/middleware"
	"net/http"
)

var (
	confFile string
)

func initArgs() {
	flag.StringVar(&confFile, "config", "./config/config.json", "指定config.json")
	flag.Parse()
}

func main() {
	var (
		e          *echo.Echo
		queueRoute *echo.Group
		err        error
	)

	e = echo.New()
	e.Use(middleware.Logger())

	//初始化命令行
	initArgs()

	//加载配置
	if err = config.InitConfig(confFile); err != nil {
		fmt.Println("加载配置文件错误:", err.Error())
		panic(err)
	}

	// 启动日志协程
	if err = processor.InitLogSink(); err != nil {
		panic(err)
	}

	// 启动调度器
	if err = processor.InitScheduler(); err != nil {
		panic(err)
	}

	e.GET("/", func(c echo.Context) error {
		return c.String(http.StatusOK, "Hello, BabyDoge")
	})

	queueRoute = e.Group("queue")
	if err = routers.QueueRoute(queueRoute); err != nil {
		panic(err)
	}

	if config.G_config.Run == "true" {
		fmt.Println("执行同步。。。")
		if err = operators.Run(); err != nil {
			fmt.Println("读取未执行的任务到内存任务失败")
			panic(err)
		}
	}

	e.Logger.Fatal(e.Start(config.G_config.Ip + ":" + config.G_config.Port))

}
